package com.qeegoo.b2bautozityp.moudle.good.viewmodel;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.orhanobut.logger.Logger;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.good.adapter.BrandAdapter;
import com.qeegoo.b2bautozityp.rxbus.IRxBusListener;
import com.qeegoo.b2bautozityp.rxbus.RxBus;
import com.qeegoo.b2bautozityp.rxbus.event.CommonEvent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;

import base.lib.util.ActivityManager;
import base.lib.widget.Floating.AppWrapperExpandableListAdapter;
import rx.Subscription;
import rx.functions.Action1;

/**
 * 王少岩 在 2017/9/19 创建了它
 */

public class GoodsBrandViewModel implements IRxBusListener {
    private ArrayList<String> letters = new ArrayList<>();
    private JSONObject brandList = new JSONObject();
    private BrandAdapter adapter;
    private AppWrapperExpandableListAdapter wrapperAdapter;
    private RequestApi mRequestApi;
    private RxBus mRxBus;

    public GoodsBrandViewModel(RxBus rxBus, RequestApi requestApi) {
        mRxBus = rxBus;
        mRequestApi = requestApi;
        adapter = new BrandAdapter(ActivityManager.getActivity(), letters, brandList);
        wrapperAdapter = new AppWrapperExpandableListAdapter(adapter);
        getBrandByKeyWords("");
    }

    private void getBrandByKeyWords(String keyword) {
        mRequestApi.listBrand(HttpParams.goodsBrandBykeyWords(keyword))
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc1())
                .subscribe(new ProgressSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        try {
                            JSONObject brand = new JSONObject(s).getJSONObject("brandList");
                            brandList.remove("brandList");
                            brandList.put("brandList", brand);
                            letters.clear();
                            Iterator<String> iterable = brand.keys();
                            while (iterable.hasNext()) {
                                letters.add(iterable.next());
                            }
                            wrapperAdapter.notifyDataSetChanged();
                            mRxBus.post(new CommonEvent(CommonEvent.FLAG_COMPLETE));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public AppWrapperExpandableListAdapter getWrapperAdapter() {
        return wrapperAdapter;
    }

    public ArrayList<String> getLetters() {
        return letters;
    }

    @Override
    public <T> void registerRxBus(Class<T> eventType, Action1<T> action) {
        Subscription subscription = mRxBus.doSubscribe(eventType, action, throwable -> Logger.e(throwable.toString()));
        mRxBus.addSubscription(this, subscription);
    }

    @Override
    public void unregisterRxBus() {
        mRxBus.unSubscribe(this);
    }

    public ReplyCommand leftCommand = new ReplyCommand(() -> ActivityManager.getActivity().finish());
    public ReplyCommand<String> afterCommand = new ReplyCommand<>(this::getBrandByKeyWords);
}
